<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2017/6/2
 * Time: 11:39
 */

namespace pc\controllers;


use yii;
use common\controllers\ComController;
use common\models\member\Members;
class MemberController extends ComController
{

    public function actionLogin(){
        if(Yii::$app->session->get("member_name")){
            return $this -> redirect(['site/index']);
        }
        $model  = new Members();
        $post = Yii::$app -> request->post();
        if (Yii::$app -> request -> isPost && $model -> load($post)) {
            if($res2 = $model -> login($post)){
                Yii::$app->session->set("member_name",$post['Members']['user']);
                Yii::$app->session->set("member_id",$res2['id']);
                Members::updateAll(['on_line'=>1],['id'=>$res2['id']]);
                return $this -> redirect(['chat2/index']);
            }else{
                echo "登录失败";exit;
            }
        }
        return $this -> render('index', ['model' => $model]);
    }
    public function actionRegister(){
        $model  = new Members();
        $post = Yii::$app -> request->post();
        if (Yii::$app -> request -> isPost && $model -> load($post)) {
            if($res2= $model -> register($post)){
                Yii::$app->session->set("member_name",$post['Members']['user']);
                Yii::$app->session->set("member_id",$res2);
                return $this -> redirect(['chat2/index']);
            }else{
                echo "注册失败";exit;
            }
        }
        return $this -> render('register', ['model' => $model]);
    }


}